package com.secneo.appshare.threads;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.secneo.appshare.api.RemoteApi.Feed;
import com.secneo.appshare.api.RemoteApi.Feeds;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;

public class FeedsThread extends Thread 
{
	private final static String TAG = "MyTag";
	private Handler handler;
	private Context ctx;
	private int number;
	private String uid;
	private String feed_type;
	private int feed_get_type;
	private String phone_type;
	
	private Feeds mFeeds;
	private boolean running = true;
	
	public FeedsThread(Context ctx, Handler handler,int number, String uid, String feed_type, int feed_get_type) 
	{
		this.handler = handler;
		this.ctx = ctx;
		this.number = number;
		this.uid = uid;
		this.feed_type = feed_type;
		this.feed_get_type = feed_get_type;
	}
	
	public synchronized void stopRun()
	{
		running = false;
		this.interrupt();
	}
	
	public Feeds getFeeds() {
		return mFeeds;
	}
	
	public void setPhoneType(String phone_type) {
		this.phone_type = phone_type;
	}
	
	public void run() 
	{
		try
		{
			running = true;
			RemoteApiImpl remoteApi = new RemoteApiImpl();
			mFeeds = null;

			if(feed_get_type==Constants.feedGetTypeAll){
				if(feed_type.equals(Constants.USER_TYPE_REQUEST_ATTENT)){
					mFeeds = remoteApi.getNotAttentFeeds(ctx, uid, number);
				}
				else if(feed_type.equals(Constants.USER_TYPE_RECEIVED_ATTENT)){
					mFeeds = remoteApi.getNeedAttentFeeds(ctx, uid, number);
				}
				else{
					mFeeds = remoteApi.getFeeds(ctx, uid, feed_type, number);
				}
			}
			else if(feed_get_type==Constants.feedGetTypeByPhone){
				mFeeds = remoteApi.getFeedsByPhone(ctx, uid, number, phone_type);
			}
			
			if (mFeeds == null) 
			{
				if(Constants.feedPageFlag)
				{
					Constants.feedPageFlag=false;
				}
				else
				{
					Message msg = new Message();
					msg.what = Constants.MSG_GET_FEEDS_NETWORK_ERROR;
					handler.sendMessage(msg);
				}
				return;
			}
			else
			{  
				Constants.feedPageFlag=true;
			}
			
			if(!running)
				return;
			Message msg = new Message();
			if(feed_type.equals(Constants.USER_TYPE_FIELD))
				msg.what = Constants.MSG_GET_FEEDS_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_MY_ATTENT))
				msg.what = Constants.MSG_GET_MY_ATTENT_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_REQUEST_ATTENT))
				msg.what = Constants.MSG_GET_REQUEST_ATTENT_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_RECEIVED_ATTENT))
				msg.what = Constants.MSG_GET_RECEIVED_ATTENT_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_MY_FANS))
				msg.what = Constants.MSG_GET_MY_FANS_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_SINA_FANS))
				msg.what = Constants.MSG_GET_SINA_FANS_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_FOLLOWED))
				msg.what = Constants.MSG_GET_FOLLOWED_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_FOLLOWER))
				msg.what = Constants.MSG_GET_FANS_FINISH;
			else if(feed_type.equals(Constants.USER_TYPE_SEARCH))
				msg.what = Constants.MSG_GET_FEEDS_FINISH;
			handler.sendMessage(msg);
			
			List<Feed> list = mFeeds.feeds;
			for(int i = 0; i< list.size() && running; i++) 
			{
				Feed f = list.get(i);
				String url = f.user_icon;
				
				try
				{
					if(url.contains("buildin"))
						f.user_icon_drawable = Util.getDrawableFromAsset(ctx, url);
					else
					    f.user_icon_drawable = Util.getDrawableFromCache(ctx, url);
				
				}catch(Exception ex) {
					Log.d(TAG, "FeedsThread::Run() read icon error = "+ex.getMessage());
				}
				
				if(f.user_icon_drawable != null)
				{
					Message iconmsg = new Message();
					iconmsg.what = Constants.MSG_GET_FEEDICON_FINISH;
					handler.sendMessage(iconmsg);
				}
			}
				
		} catch (Exception ex) {
			Log.d(TAG, "FeedsThread::Run() error = "+ex.getMessage());
			Message msg = new Message();
			msg.what = Constants.MSG_GET_FEEDS_NETWORK_ERROR;
			handler.sendMessage(msg);
		}
	}
}
